package com.iams.manage.service.impl;

import com.iams.activiti8.service.impl.WorkFlowStarterImpl;
import com.iams.common.utils.DateUtils;
import com.iams.common.utils.uuid.UUID;
import com.iams.manage.domain.workflow.DestroyRecord;
import com.iams.manage.domain.workflow.dto.DestroyRecordDTO;
import com.iams.manage.mapper.DestroyRecordMapper;
import com.iams.manage.service.IDestroyRecordService;
import com.iams.manage.service.impl.converter.DestroyRecordConverterImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DestroyRecordServiceImpl implements IDestroyRecordService {


    @Autowired
    private DestroyRecordMapper destroyRecordMapper;

    @Autowired
    private DestroyRecordConverterImpl destroyRecordConverterImpl;

    @Autowired
    private WorkFlowStarterImpl workFlowStarter;

    /**
     * 查询档案销毁
     *
     * @param id 档案销毁主键
     * @return 档案销毁
     */
    @Override
    public DestroyRecordDTO selectDestroyRecordById(Long id)
    {
        return destroyRecordConverterImpl.entityToDto(destroyRecordMapper.selectDestroyRecordById(id));
    }

    /**
     * 查询档案销毁列表
     *
     * @param archiveDestroy 档案销毁
     * @return 档案销毁
     */
    @Override
    public List<DestroyRecordDTO> selectDestroyRecordList(DestroyRecord archiveDestroy)
    {
        List<DestroyRecord> list = destroyRecordMapper.selectDestroyRecordList(archiveDestroy);

        return list.stream().map(destroyRecordEntity -> destroyRecordConverterImpl.entityToDto(destroyRecordEntity)).collect(Collectors.toList());
    }

    /**
     * 新增档案销毁
     *
     * @param archiveDestroyDTO 档案销毁
     * @return 结果
     */
    @Override
    public int insertDestroyRecord(DestroyRecordDTO archiveDestroyDTO)
    {
        // 档案验证注解：是否存在、状态不是销毁，那就可以销毁
        // 销毁请求需要验证，如果存在，则不能重复发起销毁申请
        List<DestroyRecord> list = destroyRecordMapper.selectDestroyRecordList(new DestroyRecord(archiveDestroyDTO.getUserId(), archiveDestroyDTO.getArchiveId()));
        for(DestroyRecord destroyRecord : list)
        {
            if(destroyRecord.getStatus().contains("审核"))
            {
                throw new RuntimeException("请勿重复发起申请");
            }
        }
        DestroyRecord archiveDestroy = destroyRecordConverterImpl.dtoToEntity(archiveDestroyDTO);
        archiveDestroy.setBusinessKey(UUID.randomUUID().toString());

        String instanceId = workFlowStarter.startProcessInstanceByDefinitionKey(archiveDestroy.getProcessName(), archiveDestroy.getBusinessKey(), archiveDestroyDTO.getUserName()+"销毁申请");
        archiveDestroy.setInstanceId(instanceId);
        archiveDestroy.setStartApplyTime(DateUtils.getNowDate());
        archiveDestroy.setStatus("待审批");
        return destroyRecordMapper.insertDestroyRecord(archiveDestroy);
    }

    /**
     * 修改档案销毁
     *
     * @param archiveDestroy 档案销毁
     * @return 结果
     */
    @Override
    public void updateDestroyRecord(DestroyRecord archiveDestroy)
    {
        destroyRecordMapper.updateDestroyRecord(archiveDestroy);
    }


    @Override
    public int updateDestroyRecord(DestroyRecordDTO archiveDestroyDTO)
    {
        DestroyRecord archiveDestroy = destroyRecordConverterImpl.dtoToEntity(archiveDestroyDTO);
        return destroyRecordMapper.updateDestroyRecord(archiveDestroy);
    }

    /**
     * 批量删除档案销毁
     *
     * @param ids 需要删除的档案销毁主键
     * @return 结果
     */
    @Override
    public int deleteDestroyRecordByIds(Long[] ids)
    {
        for (Long id : ids)
            deleteDestroyRecordById(id);
        return 1;
    }

    /**
     * 删除档案销毁信息
     *
     * @param id 档案销毁主键
     * @return 结果
     */
    @Override
    public int deleteDestroyRecordById(Long id)
    {
        workFlowStarter.deleteProcessInstance(destroyRecordMapper.selectDestroyRecordById(id).getInstanceId());
        return destroyRecordMapper.deleteDestroyRecordById(id);
    }

    @Override
    public DestroyRecord selectDestroyRecordByBusinessKey(String businessKey) {
        return destroyRecordMapper.selectBorrowRecordByBusinessKey(businessKey);
    }
}
